package com.henzil.siji;

import java.util.ArrayList;
import java.util.List;

import com.henzil.siji.util.Global;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class CarImagesPickerAdapter extends BaseAdapter{

	List<Integer> imageList = new ArrayList<Integer>(); 
	
	private CarImagesPickerActivity context;
	
	public CarImagesPickerAdapter(Context context ) {
		super();
		this.context = (CarImagesPickerActivity) context;
	} 
	
	@Override
	public int getCount() {
		return imageList.size();
	}

	@Override
	public Object getItem(int position) {
		return imageList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final int indext = position;
		int imageId = imageList.get(position);
		if(convertView == null){
			convertView = context.getLayoutInflater().inflate(R.layout.car_image_grid_item,parent, false);
		}
		ImageView imageView = (ImageView) convertView.findViewById(R.id.car_image);
		imageView.setImageResource(imageId);
		imageView.setTag(imageId);
		imageView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				int id = (Integer) v.getTag();
				context.selectImage.setImageResource(id);
				context.vehiclesInfo.setImageId(indext);
			}
		});
		return convertView;
	}
	
	private void getList(){
		
		imageList = Global.getImageIdList();
		
		notifyDataSetChanged();
	}
	
	public void refresh(){
		imageList.clear();
		getList();
	}

}
